package com.tianqu.tms.www.car.controller;

import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.security.TmsUserProfile;
import com.tianqu.tms.core.util.DateUtil;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.core.webservice.weichat.api.MobileMessage;
import com.tianqu.tms.core.webservice.weichat.api.PullingExecutor;
import com.tianqu.tms.core.webservice.weichat.api.rpc.TransferDataSyn30800;
import com.tianqu.tms.dto.TmsFragileStickerHistoryDto;
import com.tianqu.tms.entity.Car;
import com.tianqu.tms.entity.TmsFragileStickerHistory;
import com.tianqu.tms.www.car.viewhelper.CarStockModifyViewhelper;

import java.text.SimpleDateFormat;

public class LicenseplateModifyResultController extends TmsController {

	@Override
	public void execute(UserContext uc) throws Exception {
		TxController controller = TxController.getTxController();
		try {
			controller.begin();
			
			CarStockModifyViewhelper viewhelper = new CarStockModifyViewhelper();
			viewhelper = (CarStockModifyViewhelper)uc.getAttribute(TmsConstants.VIEWHELP);
			Car car = Car.getInstance();
			String tmpLicenseplate = viewhelper.getmTempLicensePlate() == null ? "" : viewhelper.getmTempLicensePlate().toUpperCase();
			car.updateLicenseplateBySerial(viewhelper.getmLicensePlate(),tmpLicenseplate,viewhelper.getSerial());
			
			//插入易碎贴发放记录表，如果更新后易碎贴编号有值情况
			if(!StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())){
				TmsUserProfile profile = uc.getUserProfile();
				TmsFragileStickerHistory tmsFragileStickerHistory = TmsFragileStickerHistory.getInstance();
				TmsFragileStickerHistoryDto dto = new TmsFragileStickerHistoryDto();
				dto.setIsPay(viewhelper.getIsPay()+"");
				dto.setMemberId(viewhelper.getMemberId());
				dto.setPutType("2");
				dto.setSerial(viewhelper.getSerial());
				dto.setFragileStickerId(viewhelper.getmTempLicensePlate().toUpperCase());
				dto.setInsertUserId(profile.getUniqueId());
				tmsFragileStickerHistory.insert(dto);
			}
			
			//库存车辆特殊处理记录表需要的数据
			car.setSerial(viewhelper.getSerial());
			int handleType = 0;
			if(!StringUtil.isNullOrBlank(viewhelper.getmLicensePlate())&&StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())){			
				handleType = 10;
			}else if(StringUtil.isNullOrBlank(viewhelper.getmLicensePlate())&&!StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())){
				handleType = 9;
			}else if(!StringUtil.isNullOrBlank(viewhelper.getmLicensePlate())&&!StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())){
				handleType = 11;
			}
			car.setHandleType(handleType);
			car.setIsPush(2);
			car.setPushContent("");
			TmsUserProfile profile = uc.getUserProfile();
			car.setUpdateUserId(profile.getUniqueId());
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式 
			String updataTime = DateUtil.getCurrentTime();
			car.setUpdateTime(df.parse(updataTime));
			
			//插入库存车辆特殊处理记录表
			car.insertCarHandleInfo(car);
			
			controller.commit();
			
			if(!StringUtil.isNullOrBlank(viewhelper.getmLicensePlate()) || !StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())){
				//同步接口
				MobileMessage message = new MobileMessage();
				message.putValue("Serial", viewhelper.getSerial());
				message.setMessage("【TMS】易碎贴/车牌修改==>【车代办】同步,Serial为#Serial#");
				TransferDataSyn30800 api = new TransferDataSyn30800();
				api.setMobileMessage(message);
				api.setLicensePlate(viewhelper.getmLicensePlate());
				if(!StringUtil.isNullOrBlank(viewhelper.getmTempLicensePlate())) api.setTempLicensePlate(viewhelper.getmTempLicensePlate());
				api.setSerial(viewhelper.getSerial());
				api.setMarketNo(TmsConstants.MARKET_NO);
				
				PullingExecutor.getInstance().submit(api);
			}
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}
	}
}
